package cn.chinasyq.photoquan.photo.mode;

import android.content.Context;
import android.net.Uri;

import com.ta.internal.multipart.upload.HttpType;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import cn.chinasyq.photoquan.NetworkConstant;
import cn.chinasyq.photoquan.ParamConstant;
import cn.chinasyq.photoquan.photo.bean.CommentsPojo;
import cn.chinasyq.photoquan.photo.bean.GameInfo;
import cn.chinasyq.photoquan.photo.bean.GameInfoPojo;
import cn.chinasyq.photoquan.photo.bean.GameInfosPojo;
import cn.chinasyq.photoquan.photo.bean.PhotoInfo;
import cn.chinasyq.photoquan.photo.bean.PhotoInfoPojo;
import cn.chinasyq.photoquan.photo.bean.PhotoListEntity;
import cn.chinasyq.photoquan.photo.bean.PhotoListPojo;
import cn.chinasyq.photoquan.user.activity.PhotosEntity;
import cn.chinasyq.photoquan.user.bean.AlbumPojo;
import cn.chinasyq.photoquan.user.mode.UploadFileModel;
import cn.chinasyq.photoquan.user.mode.UserMode;
import cn.chinasyq.photoquan.util.BitmapUtil;
import cn.master.volley.commons.CacheHelper;
import cn.master.volley.commons.VolleyHelper;
import cn.master.volley.models.pojo.Wrapper;
import cn.master.volley.models.response.handler.ResponseHandler;
import cn.master.volley.models.response.listener.ResolveJsonHelper;
import cn.master.volley.models.response.listener.ResponseForMultiPartListener;
import cn.master.volley.models.response.listener.ResponseListener;

/**
 * Created by 一搏 on 2015/11/26.
 */
public class PhotoMode {
    private static int page;
    public static final int MODE_REFRESH = 1;
    public static final int MODE_LOAD_MORE = 2;

    /**
     * 资讯列表banner+list
     *
     * @param handler
     * @param mode    obtain data mode
     */
    public static void obtainPhotoList(ResponseHandler handler, int mode) {
        switch (mode) {
            case MODE_LOAD_MORE:
                page++;
                break;
            case MODE_REFRESH:
                page = 1;
                break;
            default:
                break;
        }
        String url = String.format(NetworkConstant.API_PHOTOGRAPH_LIST, page);
        VolleyHelper.invalidateCache(url);
        ResponseListener<PhotoListEntity> listener = new ResponseListener<>(
                handler, ResolveJsonHelper.resolveJson2POJO(PhotoListPojo.class));
        VolleyHelper.get(url, true, listener, listener);
    }

    public static void obtainGamePhotoList(ResponseHandler handler, int mode, String uuid, String sort_type) {
        switch (mode) {
            case MODE_LOAD_MORE:
                page++;
                break;
            case MODE_REFRESH:
                page = 1;
                break;
            default:
                break;
        }
        String url = String.format(NetworkConstant.API_GAME_INDEX, uuid, sort_type, page);
        url = Uri.decode(url);
        VolleyHelper.invalidateCache(url);
        ResponseListener<ArrayList<GameInfo>> listener = new ResponseListener<>(
                handler, ResolveJsonHelper.resolveJson2POJO(GameInfosPojo.class));
        VolleyHelper.get(url, true, listener, listener);
    }

    public static void searchGamePhotoList(ResponseHandler handler, int mode, String uuid, String keyword) {
        switch (mode) {
            case MODE_LOAD_MORE:
                page++;
                break;
            case MODE_REFRESH:
                page = 1;
                break;
            default:
                break;
        }
        String url = String.format(NetworkConstant.API_GAME_SEARCH, uuid, Uri.encode(keyword), page);
        VolleyHelper.invalidateCache(url);
        ResponseListener<ArrayList<GameInfo>> listener = new ResponseListener<>(
                handler, ResolveJsonHelper.resolveJson2POJO(GameInfosPojo.class));
        VolleyHelper.get(url, true, listener, listener);
    }

    public static void obtainPhotoCommentList(Context c, ResponseHandler handler, String uuid, String cmode, int mode) {
        switch (mode) {
            case MODE_LOAD_MORE:
                page++;
                break;
            case MODE_REFRESH:
                page = 1;
                break;
            default:
                break;
        }
        String url = String.format(NetworkConstant.API_COMMENT_LIST, uuid, cmode, page, UserMode.getCurrentUserInfo(c).getToken());
        VolleyHelper.invalidateCache(url);
        ResponseListener<ArrayList<cn.chinasyq.photoquan.photo.bean.CommentsEntity>> listener = new ResponseListener<>(
                handler, ResolveJsonHelper.resolveJson2POJO(CommentsPojo.class));
        VolleyHelper.get(url, true, listener, listener);
    }

    public static void searchPhotoList(ResponseHandler handler, String keyword) {
        String url = String.format(NetworkConstant.API_PHOTOGRAPH_SEARCH, keyword);
        VolleyHelper.invalidateCache(url);
        ResponseListener<PhotoListEntity> listener = new ResponseListener<>(
                handler, ResolveJsonHelper.resolveJson2POJO(PhotoListPojo.class));
        VolleyHelper.get(url, true, listener, listener);
    }

    public static PhotoListEntity getPhotoListByCache() {
        String url = String.format(NetworkConstant.API_PHOTOGRAPH_LIST, 1);
        return CacheHelper.getCache(url, ResolveJsonHelper.resolveJson2POJO(PhotoListPojo.class));
    }

    public static ArrayList<GameInfo> getGamePhotoListByCache(Object uuid, Object sort_type) {
        String url = String.format(NetworkConstant.API_GAME_INDEX, uuid, sort_type, 1);
        return CacheHelper.getCache(url, ResolveJsonHelper.resolveJson2POJO(GameInfosPojo.class));
    }

    public static void obtainPhotoInfo(String token,String uuid,
                                       ResponseHandler handler) {
        String url = String.format(NetworkConstant.API_PHOTOGRAPH_INFO, uuid,token);
        ResponseListener<PhotoInfo> listener = new ResponseListener<>(
                handler,
                ResolveJsonHelper.resolveJson2POJO(PhotoInfoPojo.class));
        VolleyHelper.invalidateCache(url);
        VolleyHelper.get(url, true, listener, listener);
    }

    public static void obtainAlbumInfo(String token, String uuid,
                                       ResponseHandler handler) {
        String url = String.format(NetworkConstant.API_MY_PHOTOS, uuid,token);
        ResponseListener<ArrayList<PhotosEntity>> listener = new ResponseListener<>(
                handler,
                ResolveJsonHelper.resolveJson2POJO(AlbumPojo.class));
        VolleyHelper.invalidateCache(url);
        VolleyHelper.get(url, true, listener, listener);
    }

    public static void obtainGamePhotoInfo(Context c, String uuid,
                                           ResponseHandler handler) {
        String url = String.format(NetworkConstant.API_GAME_SHOW, uuid, UserMode.getCurrentUserInfo(c).getToken());
        ResponseListener<GameInfo> listener = new ResponseListener<>(
                handler,
                ResolveJsonHelper.resolveJson2POJO(GameInfoPojo.class));
        VolleyHelper.invalidateCache(url);
        VolleyHelper.get(url, true, listener, listener);
    }

    public static void upComment(Context c, ResponseHandler handler, String uuid, String type) {
        String url = NetworkConstant.API_LIKE_LIKE;
        ResponseListener<Object> listener = new ResponseListener<>(
                handler, ResolveJsonHelper.resolveAndIgnoreData);
        HashMap<String, String> params = new HashMap<>();
        params.put(ParamConstant.POST_PARAMS_TOKEY, UserMode.getCurrentUserInfo(c).getToken() + "");
        params.put(ParamConstant.POST_PARAMS_ITEM_UUID, uuid);
        params.put(ParamConstant.POST_PARAMS_ITEM_TYPE, type);
        VolleyHelper.post(url, params, listener, listener);
    }

    public static void vote(Context c, ResponseHandler handler, String uuid) {
        String url = NetworkConstant.API_GAME_VOTE;
        ResponseListener<Object> listener = new ResponseListener<>(
                handler, ResolveJsonHelper.resolveAndIgnoreData);
        HashMap<String, String> params = new HashMap<>();
        params.put(ParamConstant.POST_PARAMS_TOKEY, UserMode.getCurrentUserInfo(c).getToken() + "");
        params.put(ParamConstant.POST_PARAMS_UUID, uuid);
        VolleyHelper.post(url, params, listener, listener);
    }

    public static ResponseForMultiPartListener<Wrapper<Object>> sendComment(Context c, ResponseHandler handler, String article_uuid, String comment, Uri image, String item_type) {
        String url = NetworkConstant.API_COMMENT_CREATE;
        HashMap<String, String> params = new HashMap<>();
        params.put(ParamConstant.POST_PARAMS_TOKEY, UserMode.getCurrentUserInfo(c).getToken() + "");
        params.put(ParamConstant.POST_PARAMS_ITEM_TYPE, item_type);
        params.put(ParamConstant.POST_PARAMS_ITEM_UUID, article_uuid);
        params.put(ParamConstant.POST_PARAMS_CONTENT, comment);

        HashMap<String, File> files=new HashMap<>();
        if (image != null) {
            File file = null;
            file = new File(BitmapUtil.convertUri2FilePath(c, image));
            files.put(ParamConstant.POST_PARAMS_IMAGE,file) ;
        }
        return UploadFileModel.uploadFilesAndParams(ResolveJsonHelper.resolveAndIgnoreData, HttpType.HttpPost, url, files, params, handler);
    }
}
